<template>
  <div class="fillcontain">
    <head-top></head-top>
    <div class="table_container">
      <el-table
        :data="tableData"
        @expand="expand"
        :expand-row-keys="expendRow"
        :row-key="row => row.index"
        style="width: 100%"
      >
        <el-table-column type="expand">
          <template slot-scope="props">
            <el-form label-position="left" inline class="demo-table-expand">
              <el-form-item label="用户名">
                <span>{{ props.row.user_name }}</span>
              </el-form-item>
              <el-form-item label="用户电话">
                <span>{{ props.row.user_phone }}</span>
              </el-form-item>
              <el-form-item label="收货地址">
                <span>{{ props.row.user_address }}</span>
              </el-form-item>
              <el-form-item label="下单时间">
                <span>{{ props.row.user_time }}</span>
              </el-form-item>
            </el-form>
          </template>
        </el-table-column>
        <el-table-column label="订单 ID" prop="id"></el-table-column>
        <el-table-column label="总价格" prop="total_amount"></el-table-column>
        <el-table-column label="订单状态" prop="status"></el-table-column>
        <el-table-column label="操作" width="160">
          <template slot-scope="scope">
            <el-button size="small" @click="handleDetail(scope.row)">详情</el-button>
            <el-button
              v-if="scope.row.status=='新订单'"
              size="small"
              type="danger"
              @click="handleFinish(scope.row)"
            >完成</el-button>
          </template>
        </el-table-column>
      </el-table>

      <el-dialog title="订单详情" v-model="dialogFormVisible">
        <el-form v-model="selectTable">
          <div v-for="(item,index) of selectTable" :key="index">
            <el-form-item label="商品名称：" label-width="100px">
              <el-form-item :label="item.productName"></el-form-item>
            </el-form-item>
            <el-form-item label="商品数量：" label-width="100px">
              <el-form-item :label="item.productQuantity"></el-form-item>
            </el-form-item>
            <el-form-item label="总价：" label-width="100px">
              <el-form-item :label="item.productQuantity*item.productPrice"></el-form-item>
            </el-form-item>
          </div>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="dialogFormVisible = false">确 定</el-button>
        </div>
      </el-dialog>
      <div class="Pagination" style="text-align: left;margin-top: 10px;">
        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="currentPage"
          :page-size="20"
          layout="total, prev, pager, next"
          :total="count"
        ></el-pagination>
      </div>
    </div>
  </div>
</template>

<script>
import headTop from "../components/headTop";
import {
  getOrderList,
  getOrderCount,
  getResturantDetail,
  getUserInfo,
  getAddressById,
  finishOrder
} from "@/api/getData";
export default {
  data() {
    return {
      tableData: [],
      currentRow: null,
      offset: 0,
      limit: 20,
      count: 0,
      currentPage: 1,
      restaurant_id: null,
      expendRow: [],
      dialogFormVisible: false,
      selectTable: []
    };
  },
  components: {
    headTop
  },
  created() {
    this.restaurant_id = this.$route.query.restaurant_id;
    this.initData();
  },
  mounted() {},
  methods: {
    handleFinish(e) {
      this.finishOrder(e);
    },
    async finishOrder(e) {
      var d = {
        id: e.id
      };
      const res = await finishOrder(d);
      if (res.code == 200) {
        this.$message({
          type: "success",
          message: "订单完结成功！"
        });
        this.getOrders();
      } else {
        this.$message({
          type: "error",
          message: "完结失败，请重试！"
        });
      }
    },
    handleDetail(e) {
      this.dialogFormVisible = true;
      console.log(this.tableData[e.index]);
      this.selectTable = this.tableData[e.index].detail;
    },
    async initData() {
      try {
        // const countData = await getOrderCount({restaurant_id: this.restaurant_id});
        // if (countData.status == 1) {
        //     this.count = countData.count;
        // }else{
        //     throw new Error('获取数据失败');
        // }
        this.getOrders();
      } catch (err) {
        console.log("获取数据失败", err);
      }
    },
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
    },
    handleCurrentChange(val) {
      this.currentPage = val;
      this.offset = (val - 1) * this.limit;
      this.getOrders();
    },
    async getOrders() {
      const Orders = await getOrderList();
      this.tableData = [];
      Orders.data.forEach((item, index) => {
        const tableData = {};
        tableData.id = item.orderId;
        tableData.total_amount = item.orderAmount;
        if (item.orderStatus == 0) {
          tableData.status = "新订单";
        } else if (item.orderStatus == 1) {
          tableData.status = "已完成";
        }
        tableData.user_name = item.buyerName;
        tableData.user_phone = item.buyerPhone;
        tableData.user_address = item.buyerAddress;
        tableData.user_time = item.createTime;
        tableData.user_id = item.buyerId;
        tableData.restaurant_id = item.restaurant_id;
        tableData.address_id = item.address_id;
        tableData.index = index;
        tableData.detail = item.details;
        this.tableData.push(tableData);
      });
    },
    async expand(row, status) {
      console.log(row);
      if (status) {
        this.tableData.splice(row.index, 1, {
          ...row
        });

        this.$nextTick(() => {
          this.expendRow.push(row.index);
        });
      } else {
        const index = this.expendRow.indexOf(row.index);
        this.expendRow.splice(index, 1);
      }
    }
  }
};
</script>

<style lang="less">
@import "../style/mixin";
.table_container {
  padding: 20px;
}
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>
